home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / gdb36p4b.zoo / readline / doc / readline.info < prev   
Encoding:
GNU Info File  |  1993-10-05  |  55.8 KB  |  1,606 lines

  1. This is Info file readline.info, produced by Makeinfo-1.55 from the
  2. input file rlman.texinfo.
  3.  
  4.    This document describes the GNU Readline Library, a utility for
  5. aiding in the consitency of user interface across discrete programs
  6. that need to provide a command line interface.
  7.  
  8.    Copyright (C) 1988, 1991 Free Software Foundation, Inc.
  9.  
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice pare
  12. preserved on all copies.
  13.  
  14.    Permission is granted to copy and distribute modified versions of
  15. this manual under the conditions for verbatim copying, provided that
  16. the entire resulting derived work is distributed under the terms of a
  17. permission notice identical to this one.
  18.  
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that this permission notice may be stated in a
  22. translation approved by the Foundation.
  23.  
  24. File: readline.info,  Node: Top,  Next: Command Line Editing,  Prev: (DIR),  Up: (DIR)
  25.  
  26. GNU Readline Library
  27. ********************
  28.  
  29.    This document describes the GNU Readline Library, a utility for
  30. aiding in the consistency of user interface across discrete programs
  31. that need to provide a command line interface.
  32.  
  33. * Menu:
  34.  
  35. * Command Line Editing::       GNU Readline User's Manual.
  36. * Programming with GNU Readline::  GNU Readline Programmer's Manual.
  37. * Concept Index::           Index of concepts described in this manual.
  38. * Function and Variable Index::       Index of externally visible functions
  39.                    and variables.
  40.  
  41. File: readline.info,  Node: Command Line Editing,  Next: Programming with GNU Readline,  Prev: Top,  Up: Top
  42.  
  43. Command Line Editing
  44. ********************
  45.  
  46.    This text describes GNU's command line editing interface.
  47.  
  48. * Menu:
  49.  
  50. * Introduction and Notation::    Notation used in this text.
  51. * Readline Interaction::    The minimum set of commands for editing a line.
  52. * Readline Init File::        Customizing Readline from a user's view.
  53.  
  54. File: readline.info,  Node: Introduction and Notation,  Next: Readline Interaction,  Up: Command Line Editing
  55.  
  56. Introduction to Line Editing
  57. ============================
  58.  
  59.    In this tex a the following notation is used to describe keystrokes.
  60.  
  61.    The text C-k is read as `Control-K' and describes the character
  62. produced when the Control key is depressed and the k key is struck.
  63.  
  64.    The text M-k is read as `Meta-K' and describes the character
  65. produced when the meta key (if you have one) is depressed, and the k
  66. key is struck.  If you do not have a meta key, the identical keystroke
  67. can be generated by typing ESC first, and then typing k.  Either
  68. process is known as "metafying" the k key.
  69.  
  70.    The text M-C-k is read as `Meta-Control-k' and describes the
  71. character produced by "metafying" C-k.
  72.  
  73.    In addition, several keys have their own names.  Specifically, DEL,
  74. ESC, LFD, SPC, RET, and TAB all stand for themselves when seen in this
  75. text, or in an init file (*note Readline Init File::., for more info).
  76.  
  77. File: readline.info,  Node: Readline Interaction,  Next: Readline Init File,  Prev: Introduction and Notation,  Up: Command Line Editing
  78.  
  79. Readline Interaction
  80. ====================
  81.  
  82.    Often during an interactive session you type in a long line of text,
  83. only to notice that the first word on the line is misspelled.  The
  84. Readline library gives you a set of commands for manipulating the text
  85. as you type it in, allowing you to just fix your typo, and not forcing
  86. you to retype the majority of the line.  Using these editing commands,
  87. you move the cursor to the place that needs correction, and delete or
  88. insert the text of the corrections.  Then, when you are satisfied with
  89. the line, you simply press RETURN.  You do not have to be at the end of
  90. the line to press RETURN; the entire line is accepted regardless of the
  91. location of the cursor within the line.
  92.  
  93. * Menu:
  94.  
  95. * Readline Bare Essentials::    The least you need to know about Readline.
  96. * Readline Movement Commands::    Moving about the input line.
  97. * Readline Killing Commands::    How to delete text, and how to get it back!
  98. * Readline Arguments::        Giving numeric arguments to commands.
  99.  
  100. File: readline.info,  Node: Readline Bare Essentials,  Next: Readline Movement Commands,  Up: Readline Interaction
  101.  
  102. Readline Bare Essentials
  103. ------------------------
  104.  
  105.    In order to enter characters into the line, simply type them.  The
  106. typed character appears where the cursor was, and then the cursor moves
  107. one space to the right.  If you mistype a character, you can use DEL to
  108. back up, and delete the mistyped character.
  109.  
  110.    Sometimes you may miss typing a character that you wanted to type,
  111. and not notice your error until you have typed several other
  112. characters.  In that case, you can type C-b to move the cursor to the
  113. left, and then correct your mistake.  Aftwerwards, you can move the
  114. cursor to the right with C-f.
  115.  
  116.    When you add text in the middle of a line, you will notice that
  117. characters to the right of the cursor get `pushed over' to make room
  118. for the text that you have inserted.  Likewise, when you delete text
  119. behind the cursor, characters to the right of the cursor get `pulled
  120. back' to fill in the blank space created by the removal of the text.  A
  121. list of the basic bare essentials for editing the text of an input line
  122. follows.
  123.  
  124. C-b
  125.      Move back one character.
  126.  
  127. C-f
  128.      Move forward one character.
  129.  
  130. DEL
  131.      Delete the character to the left of the cursor.
  132.  
  133. C-d
  134.      Delete the character underneath the cursor.
  135.  
  136. Printing characters
  137.      Insert itself into the line at the cursor.
  138.  
  139. C-_
  140.      Undo the last thing that you did.  You can undo all the way back
  141.      to an empty line.
  142.  
  143. File: readline.info,  Node: Readline Movement Commands,  Next: Readline Killing Commands,  Prev: Readline Bare Essentials,  Up: Readline Interaction
  144.  
  145. Readline Movement Commands
  146. --------------------------
  147.  
  148.    The above table describes the most basic possible keystrokes that
  149. you need in order to do editing of the input line.  For your
  150. convenience, many other commands have been added in addition to C-b,
  151. C-f, C-d, and DEL.  Here are some commands for moving more rapidly
  152. about the line.
  153.  
  154. C-a
  155.      Move to the start of the line.
  156.  
  157. C-e
  158.      Move to the end of the line.
  159.  
  160. M-f
  161.      Move forward a word.
  162.  
  163. M-b
  164.      Move backward a word.
  165.  
  166. C-l
  167.      Clear the screen, reprinting the current line at the top.
  168.  
  169.    Notice how C-f moves forward a character, while M-f moves forward a
  170. word.  It is a loose convention that control keystrokes operate on
  171. characters while meta keystrokes operate on words.
  172.  
  173. File: readline.info,  Node: Readline Killing Commands,  Next: Readline Arguments,  Prev: Readline Movement Commands,  Up: Readline Interaction
  174.  
  175. Readline Killing Commands
  176. -------------------------
  177.  
  178.    The act of "cutting" text means to delete the text from the line, and
  179. to save away the deleted text for later use, just as if you had cut the
  180. text out of the line with a pair of scissors.  There is a
  181.  
  182.    "Killing" text means to delete the text from the line, but to save
  183. it away for later use, usually by "yanking" it back into the line.  If
  184. the description for a command says that it `kills' text, then you can
  185. be sure that you can get the text back in a different (or the same)
  186. place later.
  187.  
  188.    Here is the list of commands for killing text.
  189.  
  190. C-k
  191.      Kill the text from the current cursor position to the end of the
  192.      line.
  193.  
  194. M-d
  195.      Kill from the cursor to the end of the current word, or if between
  196.      words, to the end of the next word.
  197.  
  198. M-DEL
  199.      Kill fromthe cursor the start ofthe previous word, or if between
  200.      words, to the start of the previous word.
  201.  
  202. C-w
  203.      Kill from the cursor to the previous whitespace.  This is
  204.      different than M-DEL because the word boundaries differ.
  205.  
  206.    And, here is how to "yank" the text back into the line.  Yanking is
  207.  
  208. C-y
  209.      Yank the most recently killed text back into the buffer at the
  210.      cursor.
  211.  
  212. M-y
  213.      Rotate the kill-ring, and yank the new top.  You can only do this
  214.      if the prior command is C-y or M-y.
  215.  
  216.    When you use a kill command, the text is saved in a "kill-ring".
  217. Any number of consecutive kills save all of the killed text together, so
  218. that when you yank it back, you get it in one clean sweep.  The kill
  219. ring is not line specific; the text that you killed on a previously
  220. typed line is available to be yanked back later, when you are typing
  221. another line.
  222.  
  223. File: readline.info,  Node: Readline Arguments,  Prev: Readline Killing Commands,  Up: Readline Interaction
  224.  
  225. Readline Arguments
  226. ------------------
  227.  
  228.    You can pass numeric arguments to Readline commands.  Sometimes the
  229. argument acts as a repeat count, other times it is the sign of the
  230. argument that is significant.  If you pass a negative argument to a
  231. command which normally acts in a forward direction, that command will
  232. act in a backward direction.  For example, to kill text back to the
  233. start of the line, you might type M- C-k.
  234.  
  235.    The general way to pass numeric arguments to a command is to type
  236. meta digits before the command.  If the first `digit' you type is a
  237. minus sign (-), then the sign of the argument will be negative.  Once
  238. you have typed one meta digit to get the argument started, you can type
  239. the remainder of the digits, and then the command.  For example, to give
  240. the C-d command an argument of 10, you could type M-1 0 C-d.
  241.  
  242. File: readline.info,  Node: Readline Init File,  Prev: Readline Interaction,  Up: Command Line Editing
  243.  
  244. Readline Init File
  245. ==================
  246.  
  247.    Although the Readline library comes with a set of Emacs-like
  248. keybindings, it is possible that you would like to use a different set
  249. of keybindings.  You can customize programs that use Readline by putting
  250. commands in an "init" file in your home directory.  The name of this
  251. file is `~/.inputrc'.
  252.  
  253.    When a program which uses the Readline library starts up, the
  254. `~/.inputrc' file is read, and the keybindings are set.
  255.  
  256. * Menu:
  257.  
  258. * Readline Init Syntax::    Syntax for the commands in `~/.inputrc'.
  259. * Readline Vi Mode::        Switching to `vi' mode in Readline.
  260.  
  261. File: readline.info,  Node: Readline Init Syntax,  Next: Readline Vi Mode,  Up: Readline Init File
  262.  
  263. Readline Init Syntax
  264. --------------------
  265.  
  266.    You can start up with a vi-like editing mode by placing
  267.  
  268.      `set editing-mode vi'
  269.  
  270.    in your `~/.inputrc' file.
  271.  
  272.    You can have Readline use a single line for display, scrolling the
  273. input between the two edges of the screen by placing
  274.  
  275.      `set horizontal-scroll-mode On'
  276.  
  277.    in your `~/.inputrc' file.
  278.  
  279.    The syntax for controlling keybindings in the `~/.inputrc' file is
  280. simple.  First you have to know the name of the command that you want
  281. to change.  The following pages contain tables of the command name, the
  282. default keybinding, and a short description of what the command does.
  283.  
  284.    Once you know the name of the command, simply place the name of the
  285. key you wish to bind the command to, a colon, and then the name of the
  286. command on a line in the `~/.inputrc' file.  Here is an example:
  287.  
  288.      # This is a comment line.
  289.      Meta-Rubout:    backward-kill-word
  290.      Control-u:    universal-argument
  291.  
  292. * Menu:
  293.  
  294. * Commands For Moving::        Moving about the line.
  295. * Commands For History::    Getting at previous lines.
  296. * Commands For Text::        Commands for changing text.
  297. * Commands For Killing::    Commands for killing and yanking.
  298. * Numeric Arguments::        Specifying numeric arguments, repeat counts.
  299. * Commands For Completion::    Getting Readline to do the typing for you.
  300. * Miscellaneous Commands::    Other miscillaneous commands.
  301.  
  302. File: readline.info,  Node: Commands For Moving,  Next: Commands For History,  Up: Readline Init Syntax
  303.  
  304. Commands For Moving
  305. ...................
  306.  
  307. `beginning-of-line (C-a)'
  308.      Move to the start of the current line.
  309.  
  310. `end-of-line (C-e)'
  311.      Move to the end of the line.
  312.  
  313. `forward-char (C-f)'
  314.      Move forward a character.
  315.  
  316. `backward-char (C-b)'
  317.      Move back a character.
  318.  
  319. `forward-word (M-f)'
  320.      Move forward to the end of the next word.
  321.  
  322. `backward-word (M-b)'
  323.      Move back to the start of this, or the previous, word.
  324.  
  325. `clear-screen (C-l)'
  326.      Clear the screen leaving the current line at the top of the screen.
  327.  
  328. File: readline.info,  Node: Commands For History,  Next: Commands For Text,  Prev: Commands For Moving,  Up: Readline Init Syntax
  329.  
  330. Commands For Manipulating The History
  331. .....................................
  332.  
  333. `accept-line (Newline, Return)'
  334.      Accept the line regardless of where the cursor is.  If this line is
  335.      non-empty, add it too the history list.  If this line was a history
  336.      line, then restore the history line to its original state.
  337.  
  338. `previous-history (C-p)'
  339.      Move `up' through the history list.
  340.  
  341. `next-history (C-n)'
  342.      Move `down' through the history list.
  343.  
  344. `beginning-of-history (M-<)'
  345.      Move to the first line in the history.
  346.  
  347. `end-of-history (M->)'
  348.      Move to the end of the input history, i.e., the line you are
  349.      entering!
  350.  
  351. `reverse-search-history (C-r)'
  352.      Search backward starting at the current line and moving `up'
  353.      through the history as necessary.  This is an incremental search.
  354.  
  355. `forward-search-history (C-s)'
  356.      Search forward starting at the current line and moving `down'
  357.      through the the history as neccessary.
  358.  
  359. File: readline.info,  Node: Commands For Text,  Next: Commands For Killing,  Prev: Commands For History,  Up: Readline Init Syntax
  360.  
  361. Commands For Changing Text
  362. ..........................
  363.  
  364. `delete-char (C-d)'
  365.      Delete the character under the cursor.  If the cursor is at the
  366.      beginning of the line, and there are no characters in the line, and
  367.      the last character typed was not C-d, then return EOF.
  368.  
  369. `backward-delete-char (Rubout)'
  370.      Delete the character behind the cursor.  A numeric arg says to kill
  371.      the characters instead of deleting them.
  372.  
  373. `quoted-insert (C-q, C-v)'
  374.      Add the next character that you type to the line verbatim.  This is
  375.      how to insert things like C-q for example.
  376.  
  377. `tab-insert (M-TAB)'
  378.      Insert a tab character.
  379.  
  380. `self-insert (a, b, A, 1, !, ...)'
  381.      Insert yourself.
  382.  
  383. `transpose-chars (C-t)'
  384.      Drag the character before point forward over the character at
  385.      point.  Point moves forward as well.  If point is at the end of
  386.      the line, then transpose the two characters before point.
  387.      Negative args don't work.
  388.  
  389. `transpose-words (M-t)'
  390.      Drag the word behind the cursor past the word in front of the
  391.      cursor moving the cursor over that word as well.
  392.  
  393. `upcase-word (M-u)'
  394.      Uppercase the current (or following) word.  With a negative
  395.      argument, do the previous word, but do not move point.
  396.  
  397. `downcase-word (M-l)'
  398.      Lowercase the current (or following) word.  With a negative
  399.      argument, do the previous word, but do not move point.
  400.  
  401. `capitalize-word (M-c)'
  402.      Uppercase the current (or following) word.  With a negative
  403.      argument, do the previous word, but do not move point.
  404.  
  405. File: readline.info,  Node: Commands For Killing,  Next: Numeric Arguments,  Prev: Commands For Text,  Up: Readline Init Syntax
  406.  
  407. Killing And Yanking
  408. ...................
  409.  
  410. `kill-line (C-k)'
  411.      Kill the text from the current cursor position to the end of the
  412.      line.
  413.  
  414. `backward-kill-line ()'
  415.      Kill backward to the beginning of the line.  This is normally
  416.      unbound.
  417.  
  418. `kill-word (M-d)'
  419.      Kill from the cursor to the end of the current word, or if between
  420.      words, to the end of the next word.
  421.  
  422. `backward-kill-word (M-DEL)'
  423.      Kill the word behind the cursor.
  424.  
  425. `unix-line-discard (C-u)'
  426.      Do what C-u used to do in Unix line input.  We save the killed
  427.      text on the kill-ring, though.
  428.  
  429. `unix-word-rubout (C-w)'
  430.      Do what C-w used to do in Unix line input.  The killed text is
  431.      saved on the kill-ring.  This is different than backward-kill-word
  432.      because the word boundaries differ.
  433.  
  434. `yank (C-y)'
  435.      Yank the top of the kill ring into the buffer at point.
  436.  
  437. `yank-pop (M-y)'
  438.      Rotate the kill-ring, and yank the new top.  You can only do this
  439.      if the prior command is yank or yank-pop.
  440.  
  441. File: readline.info,  Node: Numeric Arguments,  Next: Commands For Completion,  Prev: Commands For Killing,  Up: Readline Init Syntax
  442.  
  443. Specifying Numeric Arguments
  444. ............................
  445.  
  446. `digit-argument (M-0, M-1, ... M--)'
  447.      Add this digit to the argument already accumulating, or start a new
  448.      argument.  M- starts a negative argument.
  449.  
  450. `universal-argument ()'
  451.      Do what C-u does in emacs.  By default, this is not bound.
  452.  
  453. File: readline.info,  Node: Commands For Completion,  Next: Miscellaneous Commands,  Prev: Numeric Arguments,  Up: Readline Init Syntax
  454.  
  455. Letting Readline Type For You
  456. .............................
  457.  
  458. `complete (TAB)'
  459.      Attempt to do completion on the text before point.  This is
  460.      implementation defined.  Generally, if you are typing a filename
  461.      argument, you can do filename completion; if you are typing a
  462.      command, you can do command completion, if you are typing in a
  463.      symbol to GDB, you can do symbol name completion, if you are
  464.      typing in a variable to Bash, you can do variable name
  465.      completion...
  466.  
  467. `possible-completions (M-?)'
  468.      List the possible completions of the text before point.
  469.  
  470. File: readline.info,  Node: Miscellaneous Commands,  Prev: Commands For Completion,  Up: Readline Init Syntax
  471.  
  472. Some Miscellaneous Commands
  473. ...........................
  474.  
  475. `abort (C-g)'
  476.      Ding!  Stops things.
  477.  
  478. `do-uppercase-version (M-a, M-b, ...)'
  479.      Run the command that is bound to your uppercase brother.
  480.  
  481. `prefix-meta (ESC)'
  482.      Make the next character that you type be metafied.  This is for
  483.      people without a meta key.  ESC-f is equivalent to M-f.
  484.  
  485. `undo (C-_)'
  486.      Incremental undo, separately remembered for each line.
  487.  
  488. `revert-line (M-r)'
  489.      Undo all changes made to this line.  This is like typing the `undo'
  490.      command enough times to get back to the beginning.
  491.  
  492. File: readline.info,  Node: Readline Vi Mode,  Prev: Readline Init Syntax,  Up: Readline Init File
  493.  
  494. Readline Vi Mode
  495. ----------------
  496.  
  497.    While the Readline library does not have a full set of Vi editing
  498. functions, it does contain enough to allow simple editing of the line.
  499.  
  500.    In order to switch interactively between Emacs and Vi editing modes,
  501. use the command M-C-j (toggle-editing-mode).
  502.  
  503.    When you enter a line in Vi mode, you are already placed in
  504. `insertion' mode, as if you had typed an `i'.  Pressing ESC switches
  505. you into `edit' mode, where you can edit the text of the line with the
  506. standard Vi movement keys, move to previous history lines with `k', and
  507. following lines with `j', and so forth.
  508.  
  509.    This document describes the GNU Readline Library, a utility for
  510. aiding in the consitency of user interface across discrete programs
  511. that need to provide a command line interface.
  512.  
  513.    Copyright (C) 1988 Free Software Foundation, Inc.
  514.  
  515.    Permission is granted to make and distribute verbatim copies of this
  516. manual provided the copyright notice and this permission notice pare
  517. preserved on all copies.
  518.  
  519.    Permission is granted to copy and distribute modified versions of
  520. this manual under the conditions for verbatim copying, provided that
  521. the entire resulting derived work is distributed under the terms of a
  522. permission notice identical to this one.
  523.  
  524.    Permission is granted to copy and distribute translations of this
  525. manual into another language, under the above conditions for modified
  526. versions, except that this permission notice may be stated in a
  527. translation approved by the Foundation.
  528.  
  529. File: readline.info,  Node: Programming with GNU Readline,  Next: Concept Index,  Prev: Command Line Editing,  Up: Top
  530.  
  531. Programming with GNU Readline
  532. *****************************
  533.  
  534.    This manual describes the interface between the GNU Readline Library
  535. and user programs.  If you are a programmer, and you wish to include the
  536. features found in GNU Readline in your own programs, such as completion,
  537. line editing, and interactive history manipulation, this documentation
  538. is for you.
  539.  
  540. * Menu:
  541.  
  542. * Default Behaviour::    Using the default behaviour of Readline.
  543. * Custom Functions::    Adding your own functions to Readline.
  544. * Custom Completers::    Supplanting or supplementing Readline's
  545.             completion functions.
  546.  
  547. File: readline.info,  Node: Default Behaviour,  Next: Custom Functions,  Up: Programming with GNU Readline
  548.  
  549. Default Behaviour
  550. =================
  551.  
  552.    Many programs provide a command line interface, such as `mail',
  553. `ftp', and `sh'.  For such programs, the default behaviour of Readline
  554. is sufficient.  This section describes how to use Readline in the
  555. simplest way possible, perhaps to replace calls in your code to `gets
  556. ()'.
  557.  
  558.    The function `readline' prints a prompt and then reads and returns a
  559. single line of text from the user.  The line which `readline ()'
  560. returns is allocated with `malloc ()'; you should `free ()' the line
  561. when you are done with it.  The declaration for `readline' in ANSI C is
  562.  
  563.      `char *readline (char *PROMPT);'
  564.  
  565.    So, one might say
  566.      `char *line = readline ("Enter a line: ");'
  567.    in order to read a line of text from the user.
  568.  
  569.    The line which is returned has the final newline removed, so only the
  570. text of the line remains.
  571.  
  572.    If readline encounters an `EOF' while reading the line, and the line
  573. is empty at that point, then `(char *)NULL' is returned.  Otherwise,
  574. the line is ended just as if a newline was typed.
  575.  
  576.    If you want the user to be able to get at the line later, (with C-p
  577. for example), you must call `add_history ()' to save the line away in a
  578. "history" list of such lines.
  579.  
  580.      `add_history (line)';
  581.  
  582.    For full details on the GNU History Library, see the associated
  583. manual.
  584.  
  585.    It is polite to avoid saving empty lines on the history list, since
  586. it is rare than someone has a burning need to reuse a blank line.  Here
  587. is a function which usefully replaces the standard `gets ()' library
  588. function:
  589.  
  590.      /* A static variable for holding the line. */
  591.      static char *line_read = (char *)NULL;
  592.      
  593.      /* Read a string, and return a pointer to it.  Returns NULL on EOF. */
  594.      char *
  595.      do_gets ()
  596.      {
  597.        /* If the buffer has already been allocated, return the memory
  598.           to the free pool. */
  599.        if (line_read != (char *)NULL)
  600.          {
  601.            free (line_read);
  602.            line_read = (char *)NULL;
  603.          }
  604.      
  605.        /* Get a line from the user. */
  606.        line_read = readline ("");
  607.      
  608.        /* If the line has any text in it, save it on the history. */
  609.        if (line_read && *line_read)
  610.          add_history (line_read);
  611.      
  612.        return (line_read);
  613.      }
  614.  
  615.    The above code gives the user the default behaviour of TAB
  616. completion: completion on file names.  If you do not want readline to
  617. complete on filenames, you can change the binding of the TAB key with
  618. `rl_bind_key ()'.
  619.  
  620.      `int rl_bind_key (int KEY, (int (*)())FUNCTION);'
  621.  
  622.    `rl_bind_key ()' takes 2 arguments; KEY is the character that you
  623. want to bind, and FUNCTION is the address of the function to run when
  624. KEY is pressed.  Binding TAB to `rl_insert ()' makes TAB just insert
  625. itself.
  626.  
  627.    `rl_bind_key ()' returns non-zero if KEY is not a valid ASCII
  628. character code (between 0 and 255).
  629.  
  630.      `rl_bind_key ('\t', rl_insert);'
  631.  
  632.    This code should be executed once at the start of your program; you
  633. might write a function called `initialize_readline ()' which performs
  634. this and other desired initializations, such as installing custom
  635. completers, etc.
  636.  
  637. File: readline.info,  Node: Custom Functions,  Next: Custom Completers,  Prev: Default Behaviour,  Up: Programming with GNU Readline
  638.  
  639. Custom Functions
  640. ================
  641.  
  642.    Readline provides a great many functions for manipulating the text of
  643. the line.  But it isn't possible to anticipate the needs of all
  644. programs.  This section describes the various functions and variables
  645. defined in within the Readline library which allow a user program to add
  646. customized functionality to Readline.
  647.  
  648. * Menu:
  649.  
  650. * The Function Type::    C declarations to make code readable.
  651. * Function Naming::    How to give a function you write a name.
  652. * Keymaps::        Making keymaps.
  653. * Binding Keys::    Changing Keymaps.
  654. * Function Writing::    Variables and calling conventions.
  655. * Allowing Undoing::    How to make your functions undoable.
  656.  
  657. File: readline.info,  Node: The Function Type,  Next: Function Naming,  Up: Custom Functions
  658.  
  659. The Function Type
  660. -----------------
  661.  
  662.    For the sake of readabilty, we declare a new type of object, called
  663. "Function".  A `Function' is a C language function which returns an
  664. `int'.  The type declaration for `Function' is:
  665.  
  666. `typedef int Function ();'
  667.  
  668.    The reason for declaring this new type is to make it easier to write
  669. code describing pointers to C functions.  Let us say we had a variable
  670. called FUNC which was a pointer to a function.  Instead of the classic
  671. C declaration
  672.  
  673.    `int (*)()func;'
  674.  
  675.    we have
  676.  
  677.    `Function *func;'
  678.  
  679. File: readline.info,  Node: Function Naming,  Next: Keymaps,  Prev: The Function Type,  Up: Custom Functions
  680.  
  681. Naming a Function
  682. -----------------
  683.  
  684.    The user can dynamically change the bindings of keys while using
  685. Readline.  This is done by representing the function with a descriptive
  686. name.  The user is able to type the descriptive name when referring to
  687. the function.  Thus, in an init file, one might find
  688.  
  689.      Meta-Rubout:    backward-kill-word
  690.  
  691.    This binds the keystroke Meta-Rubout to the function *descriptively*
  692. named `backward-kill-word'.  You, as the programmer, should bind the
  693. functions you write to descriptive names as well.  Readline provides a
  694. function for doing that:
  695.  
  696.  - Function: rl_add_defun (CHAR *NAME, FUNCTION *FUNCTION, INT KEY)
  697.      Add NAME to the list of named functions.  Make FUNCTION be the
  698.      function that gets called.  If KEY is not -1, then bind it to
  699.      FUNCTION using `rl_bind_key ()'.
  700.  
  701.    Using this function alone is sufficient for most applications.  It is
  702. the recommended way to add a few functions to the default functions that
  703. Readline has built in already.  If you need to do more or different
  704. things than adding a function to Readline, you may need to use the
  705. underlying functions described below.
  706.  
  707. File: readline.info,  Node: Keymaps,  Next: Binding Keys,  Prev: Function Naming,  Up: Custom Functions
  708.  
  709. Selecting a Keymap
  710. ------------------
  711.  
  712.    Key bindings take place on a "keymap".  The keymap is the
  713. association between the keys that the user types and the functions that
  714. get run.  You can make your own keymaps, copy existing keymaps, and tell
  715. Readline which keymap to use.
  716.  
  717.  - Function: Keymap rl_make_bare_keymap ()
  718.      Returns a new, empty keymap.  The space for the keymap is
  719.      allocated with `malloc ()'; you should `free ()' it when you are
  720.      done.
  721.  
  722.  - Function: Keymap rl_copy_keymap (KEYMAP MAP)
  723.      Return a new keymap which is a copy of MAP.
  724.  
  725.  - Function: Keymap rl_make_keymap ()
  726.      Return a new keymap with the printing characters bound to
  727.      rl_insert, the lowercase Meta characters bound to run their
  728.      equivalents, and the Meta digits bound to produce numeric
  729.      arguments.
  730.  
  731. File: readline.info,  Node: Binding Keys,  Next: Function Writing,  Prev: Keymaps,  Up: Custom Functions
  732.  
  733. Binding Keys
  734. ------------
  735.  
  736.    You associate keys with functions through the keymap.  Here are
  737. functions for doing that.
  738.  
  739.  - Function: int rl_bind_key (INT KEY, FUNCTION *FUNCTION)
  740.      Binds KEY to FUNCTION in the currently selected keymap.  Returns
  741.      non-zero in the case of an invalid KEY.
  742.  
  743.  - Function: int rl_bind_key_in_map (INT KEY, FUNCTION *FUNCTION,
  744.           KEYMAP MAP)
  745.      Bind KEY to FUNCTION in MAP.  Returns non-zero in the case of an
  746.      invalid KEY.
  747.  
  748.  - Function: int rl_unbind_key (INT KEY)
  749.      Make KEY do nothing in the currently selected keymap.  Returns
  750.      non-zero in case of error.
  751.  
  752.  - Function: int rl_unbind_key_in_map (INT KEY, KEYMAP MAP)
  753.      Make KEY be bound to the null function in MAP.  Returns non-zero
  754.      in case of error.
  755.  
  756.  - Function: rl_generic_bind (INT TYPE, CHAR *KEYSEQ, CHAR *DATA,
  757.           KEYMAP MAP)
  758.      Bind the key sequence represented by the string KEYSEQ to the
  759.      arbitrary pointer DATA.  TYPE says what kind of data is pointed to
  760.      by DATA; right now this can be a function (`ISFUNC'), a macro
  761.      (`ISMACR'), or a keymap (`ISKMAP').  This makes new keymaps as
  762.      necessary.  The initial place to do bindings is in MAP.
  763.  
  764. File: readline.info,  Node: Function Writing,  Next: Allowing Undoing,  Prev: Binding Keys,  Up: Custom Functions
  765.  
  766. Writing a New Function
  767. ----------------------
  768.  
  769.    In order to write new functions for Readline, you need to know the
  770. calling conventions for keyboard invoked functions, and the names of the
  771. variables that describe the current state of the line gathered so far.
  772.  
  773.  - Variable: char *rl_line_buffer
  774.      This is the line gathered so far.  You are welcome to modify the
  775.      contents of this, but see Undoing, below.
  776.  
  777.  - Variable: int rl_point
  778.      The offset of the current cursor position in RL_LINE_BUFFER.
  779.  
  780.  - Variable: int rl_end
  781.      The number of characters present in `rl_line_buffer'.  When
  782.      `rl_point' is at the end of the line, then `rl_point' and `rl_end'
  783.      are equal.
  784.  
  785.    The calling sequence for a command `foo' looks like
  786.  
  787.      `foo (int count, int key)'
  788.  
  789.    where COUNT is the numeric argument (or 1 if defaulted) and KEY is
  790. the key that invoked this function.
  791.  
  792.    It is completely up to the function as to what should be done with
  793. the numeric argument; some functions use it as a repeat count, other
  794. functions as a flag, and some choose to ignore it.  In general, if a
  795. function uses the numeric argument as a repeat count, it should be able
  796. to do something useful with a negative argument as well as a positive
  797. argument.  At the very least, it should be aware that it can be passed a
  798. negative argument.
  799.  
  800. File: readline.info,  Node: Allowing Undoing,  Prev: Function Writing,  Up: Custom Functions
  801.  
  802. Allowing Undoing
  803. ----------------
  804.  
  805.    Supporting the undo command is a painless thing to do, and makes your
  806. functions much more useful to the end user.  It is certainly easy to try
  807. something if you know you can undo it.  I could use an undo function for
  808. the stock market.
  809.  
  810.    If your function simply inserts text once, or deletes text once, and
  811. it calls `rl_insert_text ()' or `rl_delete_text ()' to do it, then
  812. undoing is already done for you automatically, and you can safely skip
  813. this section.
  814.  
  815.    If you do multiple insertions or multiple deletions, or any
  816. combination of these operations, you should group them together into
  817. one operation.  This can be done with `rl_begin_undo_group ()' and
  818. `rl_end_undo_group ()'.
  819.  
  820.  - Function: rl_begin_undo_group ()
  821.      Begins saving undo information in a group construct.  The undo
  822.      information usually comes from calls to `rl_insert_text ()' and
  823.      `rl_delete_text ()', but they could be direct calls to
  824.      `rl_add_undo ()'.
  825.  
  826.  - Function: rl_end_undo_group ()
  827.      Closes the current undo group started with `rl_begin_undo_group
  828.      ()'.  There should be exactly one call to `rl_end_undo_group ()'
  829.      for every call to `rl_begin_undo_group ()'.
  830.  
  831.    Finally, if you neither insert nor delete text, but directly modify
  832. the existing text (e.g. change its case), you call `rl_modifying ()'
  833. once, just before you modify the text.  You must supply the indices of
  834. the text range that you are going to modify.
  835.  
  836.  - Function: rl_modifying (INT START, INT END)
  837.      Tell Readline to save the text between START and END as a single
  838.      undo unit.  It is assumed that subsequent to this call you will
  839.      modify that range of text in some way.
  840.  
  841. An Example
  842. ----------
  843.  
  844.    Here is a function which changes lowercase characters to the
  845. uppercase equivalents, and uppercase characters to the lowercase
  846. equivalents.  If this function was bound to `M-c', then typing `M-c'
  847. would change the case of the character under point.  Typing `10 M-c'
  848. would change the case of the following 10 characters, leaving the
  849. cursor on the last character changed.
  850.  
  851.      /* Invert the case of the COUNT following characters. */
  852.      invert_case_line (count, key)
  853.           int count, key;
  854.      {
  855.        register int start, end;
  856.      
  857.        start = rl_point;
  858.      
  859.        if (count < 0)
  860.          {
  861.            direction = -1;
  862.            count = -count;
  863.          }
  864.        else
  865.          direction = 1;
  866.      
  867.        /* Find the end of the range to modify. */
  868.        end = start + (count * direction);
  869.      
  870.        /* Force it to be within range. */
  871.        if (end > rl_end)
  872.          end = rl_end;
  873.        else if (end < 0)
  874.          end = -1;
  875.      
  876.        if (start > end)
  877.          {
  878.            int temp = start;
  879.            start = end;
  880.            end = temp;
  881.          }
  882.      
  883.        if (start == end)
  884.          return;
  885.      
  886.        /* Tell readline that we are modifying the line, so save the undo
  887.           information. */
  888.        rl_modifying (start, end);
  889.      
  890.        for (; start != end; start += direction)
  891.          {
  892.            if (uppercase_p (rl_line_buffer[start]))
  893.              rl_line_buffer[start] = to_lower (rl_line_buffer[start]);
  894.            else if (lowercase_p (rl_line_buffer[start]))
  895.              rl_line_buffer[start] = to_upper (rl_line_buffer[start]);
  896.          }
  897.        /* Move point to on top of the last character changed. */
  898.        rl_point = end - direction;
  899.      }
  900.  
  901. File: readline.info,  Node: Custom Completers,  Prev: Custom Functions,  Up: Programming with GNU Readline
  902.  
  903. Custom Completers
  904. =================
  905.  
  906.    Typically, a program that reads commands from the user has a way of
  907. disambiguating between commands and data.  If your program is one of
  908. these, then it can provide completion for either commands, or data, or
  909. both commands and data.  The following sections describe how your
  910. program and Readline cooperate to provide this service to end users.
  911.  
  912. * Menu:
  913.  
  914. * How Completing Works::    The logic used to do completion.
  915. * Completion Functions::    Functions provided by Readline.
  916. * Completion Variables::    Variables which control completion.
  917. * A Short Completion Example::    An example of writing completer subroutines.
  918.  
  919. File: readline.info,  Node: How Completing Works,  Next: Completion Functions,  Up: Custom Completers
  920.  
  921. How Completing Works
  922. --------------------
  923.  
  924.    In order to complete some text, the full list of possible completions
  925. must be available.  That is to say, it is not possible to accurately
  926. expand a partial word without knowing what all of the possible words
  927. that make sense in that context are.  The GNU Readline library provides
  928. the user interface to completion, and additionally, two of the most
  929. common completion functions; filename and username.  For completing
  930. other types of text, you must write your own completion function.  This
  931. section describes exactly what those functions must do, and provides an
  932. example function.
  933.  
  934.    There are three major functions used to perform completion:
  935.  
  936.   1. The user-interface function `rl_complete ()'.  This function is
  937.      called interactively with the same calling conventions as other
  938.      functions in readline intended for interactive use; i.e. COUNT,
  939.      and `invoking-key'.  It isolates the word to be completed and calls
  940.      `completion_matches ()' to generate a list of possible completions.
  941.      It then either lists the possible completions or actually performs
  942.      the completion, depending on which behaviour is desired.
  943.  
  944.   2. The internal function `completion_matches ()' uses your
  945.      "generator" function to generate the list of possible matches, and
  946.      then returns the array of these matches.  You should place the
  947.      address of your generator function in
  948.      `rl_completion_entry_function'.
  949.  
  950.   3. The generator function is called repeatedly from
  951.      `completion_matches ()', returning a string each time.  The
  952.      arguments to the generator function are TEXT and STATE.  TEXT is
  953.      the partial word to be completed.  STATE is zero the first time
  954.      the function is called, and a positive non-zero integer for each
  955.      subsequent call.  When the generator function returns `(char
  956.      *)NULL' this signals `completion_matches ()' that there are no more
  957.      possibilities left.
  958.  
  959.  
  960.  - Function: rl_complete (INT IGNORE, INT INVOKING_KEY)
  961.      Complete the word at or before point.  You have supplied the
  962.      function that does the initial simple matching selection algorithm
  963.      (see `completion_matches ()').  The default is to do filename
  964.      completion.
  965.  
  966.    Note that `rl_complete ()' has the identical calling conventions as
  967. any other key-invokable function; this is because by default it is bound
  968. to the `TAB' key.
  969.  
  970.  - Variable: Function *rl_completion_entry_function
  971.      This is a pointer to the generator function for `completion_matches
  972.      ()'.  If the value of `rl_completion_entry_function' is `(Function
  973.      *)NULL' then the default filename generator function is used,
  974.      namely `filename_entry_function ()'.
  975.  
  976. File: readline.info,  Node: Completion Functions,  Next: Completion Variables,  Prev: How Completing Works,  Up: Custom Completers
  977.  
  978. Completion Functions
  979. --------------------
  980.  
  981.    Here is the complete list of callable completion functions present in
  982. Readline.
  983.  
  984.  - Function: rl_complete_internal (INT WHAT_TO_DO)
  985.      Complete the word at or before point.  WHAT_TO_DO says what to do
  986.      with the completion.  A value of `?' means list the possible
  987.      completions.  `TAB' means do standard completion.  `*' means
  988.      insert all of the possible completions.
  989.  
  990.  - Function: rl_complete (INT IGNORE, INT INVOKING_KEY)
  991.      Complete the word at or before point.  You have supplied the
  992.      function that does the initial simple matching selection algorithm
  993.      (see `completion_matches ()').  The default is to do filename
  994.      completion.  This just calls `rl_complete_internal ()' with an
  995.      argument of `TAB'.
  996.  
  997.  - Function: rl_possible_completions ()
  998.      List the possible completions.  See description of `rl_complete
  999.      ()'.  This just calls `rl_complete_internal ()' with an argument of
  1000.      `?'.
  1001.  
  1002.  - Function: char **completion_matches (CHAR *TEXT, CHAR
  1003.           *(*ENTRY_FUNCTION) ())
  1004.      Returns an array of `(char *)' which is a list of completions for
  1005.      TEXT.  If there are no completions, returns `(char **)NULL'.  The
  1006.      first entry in the returned array is the substitution for TEXT.
  1007.      The remaining entries are the possible completions.  The array is
  1008.      terminated with a `NULL' pointer.
  1009.  
  1010.      ENTRY_FUNCTION is a function of two args, and returns a `(char
  1011.      *)'.  The first argument is TEXT.  The second is a state argument;
  1012.      it is zero on the first call, and non-zero on subsequent calls.
  1013.      It returns a `NULL'  pointer to the caller when there are no more
  1014.      matches.
  1015.  
  1016.  - Function: char *filename_completion_function (CHAR *TEXT, INT STATE)
  1017.      A generator function for filename completion in the general case.
  1018.      Note that completion in the Bash shell is a little different
  1019.      because of all the pathnames that must be followed when looking up
  1020.      the completion for a command.
  1021.  
  1022.  - Function: char *username_completion_function (CHAR *TEXT, INT STATE)
  1023.      A completion generator for usernames.  TEXT contains a partial
  1024.      username preceded by a random character (usually `~').
  1025.  
  1026. File: readline.info,  Node: Completion Variables,  Next: A Short Completion Example,  Prev: Completion Functions,  Up: Custom Completers
  1027.  
  1028. Completion Variables
  1029. --------------------
  1030.  
  1031.  - Variable: Function *rl_completion_entry_function
  1032.      A pointer to the generator function for `completion_matches ()'.
  1033.      `NULL' means to use `filename_entry_function ()', the default
  1034.      filename completer.
  1035.  
  1036.  - Variable: Function *rl_attempted_completion_function
  1037.      A pointer to an alternative function to create matches.  The
  1038.      function is called with TEXT, START, and END.  START and END are
  1039.      indices in `rl_line_buffer' saying what the boundaries of TEXT
  1040.      are.  If this function exists and returns `NULL' then `rl_complete
  1041.      ()' will call the value of `rl_completion_entry_function' to
  1042.      generate matches, otherwise the array of strings returned will be
  1043.      used.
  1044.  
  1045.  - Variable: int rl_completion_query_items
  1046.      Up to this many items will be displayed in response to a
  1047.      possible-completions call.  After that, we ask the user if she is
  1048.      sure she wants to see them all.  The default value is 100.
  1049.  
  1050.  - Variable: char *rl_basic_word_break_characters
  1051.      The basic list of characters that signal a break between words for
  1052.      the completer routine.  The contents of this variable is what
  1053.      breaks words in the Bash shell, i.e. " \t\n\"\\'`@$><=".
  1054.  
  1055.  - Variable: char *rl_completer_word_break_characters
  1056.      The list of characters that signal a break between words for
  1057.      `rl_complete_internal ()'.  The default list is the contents of
  1058.      `rl_basic_word_break_characters'.
  1059.  
  1060.  - Variable: char *rl_special_prefixes
  1061.      The list of characters that are word break characters, but should
  1062.      be left in TEXT when it is passed to the completion function.
  1063.      Programs can use this to help determine what kind of completing to
  1064.      do.
  1065.  
  1066.  - Variable: int rl_ignore_completion_duplicates
  1067.      If non-zero, then disallow duplicates in the matches.  Default is
  1068.      1.
  1069.  
  1070.  - Variable: int rl_filename_completion_desired
  1071.      Non-zero means that the results of the matches are to be treated as
  1072.      filenames.  This is *always* zero on entry, and can only be changed
  1073.      within a completion entry generator function.
  1074.  
  1075.  - Variable: Function *rl_ignore_some_completions_function
  1076.      This function, if defined, is called by the completer when real
  1077.      filename completion is done, after all the matching names have
  1078.      been generated.  It is passed a `NULL' terminated array of
  1079.      pointers to `(char *)' known as MATCHES in the code.  The 1st
  1080.      element (`matches[0]') is the maximal substring that is common to
  1081.      all matches. This function can re-arrange the list of matches as
  1082.      required, but each deleted element of the array must be `free()''d.
  1083.  
  1084. File: readline.info,  Node: A Short Completion Example,  Prev: Completion Variables,  Up: Custom Completers
  1085.  
  1086. A Short Completion Example
  1087. --------------------------
  1088.  
  1089.    Here is a small application demonstrating the use of the GNU Readline
  1090. library.  It is called `fileman', and the source code resides in
  1091. `readline/examples/fileman.c'.  This sample application provides
  1092. completion of command names, line editing features, and access to the
  1093. history list.
  1094.  
  1095.      /* fileman.c -- A tiny application which demonstrates how to use the
  1096.         GNU Readline library.  This application interactively allows users
  1097.         to manipulate files and their modes. */
  1098.      
  1099.      #include <stdio.h>
  1100.      #include <readline/readline.h>
  1101.      #include <readline/history.h>
  1102.      #include <sys/types.h>
  1103.      #include <sys/file.h>
  1104.      #include <sys/stat.h>
  1105.      #include <sys/errno.h>
  1106.      
  1107.      /* The names of functions that actually do the manipulation. */
  1108.      int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
  1109.      int com_delete (), com_help (), com_cd (), com_quit ();
  1110.      
  1111.      /* A structure which contains information on the commands this program
  1112.         can understand. */
  1113.      
  1114.      typedef struct {
  1115.        char *name;                   /* User printable name of the function. */
  1116.        Function *func;               /* Function to call to do the job. */
  1117.        char *doc;                    /* Documentation for this function.  */
  1118.      } COMMAND;
  1119.      
  1120.      COMMAND commands[] = {
  1121.        { "cd", com_cd, "Change to directory DIR" },
  1122.        { "delete", com_delete, "Delete FILE" },
  1123.        { "help", com_help, "Display this text" },
  1124.        { "?", com_help, "Synonym for `help'" },
  1125.        { "list", com_list, "List files in DIR" },
  1126.        { "ls", com_list, "Synonym for `list'" },
  1127.        { "pwd", com_pwd, "Print the current working directory" },
  1128.        { "quit", com_quit, "Quit using Fileman" },
  1129.        { "rename", com_rename, "Rename FILE to NEWNAME" },
  1130.        { "stat", com_stat, "Print out statistics on FILE" },
  1131.        { "view", com_view, "View the contents of FILE" },
  1132.        { (char *)NULL, (Function *)NULL, (char *)NULL }
  1133.      };
  1134.      
  1135.      /* The name of this program, as taken from argv[0]. */
  1136.      char *progname;
  1137.      
  1138.      /* When non-zero, this global means the user is done using this program. */
  1139.      int done = 0;
  1140.      
  1141.      main (argc, argv)
  1142.           int argc;
  1143.           char **argv;
  1144.      {
  1145.        progname = argv[0];
  1146.      
  1147.        initialize_readline ();       /* Bind our completer. */
  1148.      
  1149.        /* Loop reading and executing lines until the user quits. */
  1150.        while (!done)
  1151.          {
  1152.            char *line;
  1153.      
  1154.            line = readline ("FileMan: ");
  1155.      
  1156.            if (!line)
  1157.              {
  1158.                done = 1;             /* Encountered EOF at top level. */
  1159.              }
  1160.            else
  1161.              {
  1162.                /* Remove leading and trailing whitespace from the line.
  1163.                   Then, if there is anything left, add it to the history list
  1164.                   and execute it. */
  1165.                stripwhite (line);
  1166.      
  1167.                if (*line)
  1168.                  {
  1169.                    add_history (line);
  1170.                    execute_line (line);
  1171.                  }
  1172.              }
  1173.      
  1174.            if (line)
  1175.              free (line);
  1176.          }
  1177.        exit (0);
  1178.      }
  1179.      
  1180.      /* Execute a command line. */
  1181.      execute_line (line)
  1182.           char *line;
  1183.      {
  1184.        register int i;
  1185.        COMMAND *find_command (), *command;
  1186.        char *word;
  1187.      
  1188.        /* Isolate the command word. */
  1189.        i = 0;
  1190.        while (line[i] && !whitespace (line[i]))
  1191.          i++;
  1192.      
  1193.        word = line;
  1194.      
  1195.        if (line[i])
  1196.          line[i++] = '\0';
  1197.      
  1198.        command = find_command (word);
  1199.      
  1200.        if (!command)
  1201.          {
  1202.            fprintf (stderr, "%s: No such command for FileMan.\n", word);
  1203.            return;
  1204.          }
  1205.      
  1206.        /* Get argument to command, if any. */
  1207.        while (whitespace (line[i]))
  1208.          i++;
  1209.      
  1210.        word = line + i;
  1211.      
  1212.        /* Call the function. */
  1213.        (*(command->func)) (word);
  1214.      }
  1215.      
  1216.      /* Look up NAME as the name of a command, and return a pointer to that
  1217.         command.  Return a NULL pointer if NAME isn't a command name. */
  1218.      COMMAND *
  1219.      find_command (name)
  1220.           char *name;
  1221.      {
  1222.        register int i;
  1223.      
  1224.        for (i = 0; commands[i].name; i++)
  1225.          if (strcmp (name, commands[i].name) == 0)
  1226.            return (&commands[i]);
  1227.      
  1228.        return ((COMMAND *)NULL);
  1229.      }
  1230.      
  1231.      /* Strip whitespace from the start and end of STRING. */
  1232.      stripwhite (string)
  1233.           char *string;
  1234.      {
  1235.        register int i = 0;
  1236.      
  1237.        while (whitespace (string[i]))
  1238.          i++;
  1239.      
  1240.        if (i)
  1241.          strcpy (string, string + i);
  1242.      
  1243.        i = strlen (string) - 1;
  1244.      
  1245.        while (i > 0 && whitespace (string[i]))
  1246.          i--;
  1247.      
  1248.        string[++i] = '\0';
  1249.      }
  1250.      
  1251.      /* **************************************************************** */
  1252.      /*                                                                  */
  1253.      /*                  Interface to Readline Completion                */
  1254.      /*                                                                  */
  1255.      /* **************************************************************** */
  1256.      
  1257.      /* Tell the GNU Readline library how to complete.  We want to try to complete
  1258.         on command names if this is the first word in the line, or on filenames
  1259.         if not. */
  1260.      initialize_readline ()
  1261.      {
  1262.        char **fileman_completion ();
  1263.      
  1264.        /* Allow conditional parsing of the ~/.inputrc file. */
  1265.        rl_readline_name = "FileMan";
  1266.      
  1267.        /* Tell the completer that we want a crack first. */
  1268.        rl_attempted_completion_function = (Function *)fileman_completion;
  1269.      }
  1270.      
  1271.      /* Attempt to complete on the contents of TEXT.  START and END show the
  1272.         region of TEXT that contains the word to complete.  We can use the
  1273.         entire line in case we want to do some simple parsing.  Return the
  1274.         array of matches, or NULL if there aren't any. */
  1275.      char **
  1276.      fileman_completion (text, start, end)
  1277.           char *text;
  1278.           int start, end;
  1279.      {
  1280.        char **matches;
  1281.        char *command_generator ();
  1282.      
  1283.        matches = (char **)NULL;
  1284.      
  1285.        /* If this word is at the start of the line, then it is a command
  1286.           to complete.  Otherwise it is the name of a file in the current
  1287.           directory. */
  1288.        if (start == 0)
  1289.          matches = completion_matches (text, command_generator);
  1290.      
  1291.        return (matches);
  1292.      }
  1293.      
  1294.      /* Generator function for command completion.  STATE lets us know whether
  1295.         to start from scratch; without any state (i.e. STATE == 0), then we
  1296.         start at the top of the list. */
  1297.      char *
  1298.      command_generator (text, state)
  1299.           char *text;
  1300.           int state;
  1301.      {
  1302.        static int list_index, len;
  1303.        char *name;
  1304.      
  1305.        /* If this is a new word to complete, initialize now.  This includes
  1306.           saving the length of TEXT for efficiency, and initializing the index
  1307.           variable to 0. */
  1308.        if (!state)
  1309.          {
  1310.            list_index = 0;
  1311.            len = strlen (text);
  1312.          }
  1313.      
  1314.        /* Return the next name which partially matches from the command list. */
  1315.        while (name = commands[list_index].name)
  1316.          {
  1317.            list_index++;
  1318.      
  1319.            if (strncmp (name, text, len) == 0)
  1320.              return (name);
  1321.          }
  1322.      
  1323.        /* If no names matched, then return NULL. */
  1324.        return ((char *)NULL);
  1325.      }
  1326.      
  1327.      /* **************************************************************** */
  1328.      /*                                                                  */
  1329.      /*                       FileMan Commands                           */
  1330.      /*                                                                  */
  1331.      /* **************************************************************** */
  1332.      
  1333.      /* String to pass to system ().  This is for the LIST, VIEW and RENAME
  1334.         commands. */
  1335.      static char syscom[1024];
  1336.      
  1337.      /* List the file(s) named in arg. */
  1338.      com_list (arg)
  1339.           char *arg;
  1340.      {
  1341.        sprintf (syscom, "ls -FClg %s", arg);
  1342.        system (syscom);
  1343.      }
  1344.      
  1345.      com_view (arg)
  1346.           char *arg;
  1347.      {
  1348.        if (!valid_argument ("view", arg))
  1349.          return;
  1350.      
  1351.        sprintf (syscom, "cat %s | more", arg);
  1352.        system (syscom);
  1353.      }
  1354.      
  1355.      com_rename (arg)
  1356.           char *arg;
  1357.      {
  1358.        too_dangerous ("rename");
  1359.      }
  1360.      
  1361.      com_stat (arg)
  1362.           char *arg;
  1363.      {
  1364.        struct stat finfo;
  1365.      
  1366.        if (!valid_argument ("stat", arg))
  1367.          return;
  1368.      
  1369.        if (stat (arg, &finfo) == -1)
  1370.          {
  1371.            perror (arg);
  1372.            return;
  1373.          }
  1374.      
  1375.        printf ("Statistics for `%s':\n", arg);
  1376.      
  1377.        printf ("%s has %d link%s, and is %d bytes in length.\n", arg,
  1378.                finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s",  finfo.st_size);
  1379.        printf ("      Created on: %s", ctime (&finfo.st_ctime));
  1380.        printf ("  Last access at: %s", ctime (&finfo.st_atime));
  1381.        printf ("Last modified at: %s", ctime (&finfo.st_mtime));
  1382.      }
  1383.      
  1384.      com_delete (arg)
  1385.           char *arg;
  1386.      {
  1387.        too_dangerous ("delete");
  1388.      }
  1389.      
  1390.      /* Print out help for ARG, or for all of the commands if ARG is
  1391.         not present. */
  1392.      com_help (arg)
  1393.           char *arg;
  1394.      {
  1395.        register int i;
  1396.        int printed = 0;
  1397.      
  1398.        for (i = 0; commands[i].name; i++)
  1399.          {
  1400.            if (!*arg || (strcmp (arg, commands[i].name) == 0))
  1401.              {
  1402.                printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  1403.                printed++;
  1404.              }
  1405.          }
  1406.      
  1407.        if (!printed)
  1408.          {
  1409.            printf ("No commands match `%s'.  Possibilties are:\n", arg);
  1410.      
  1411.            for (i = 0; commands[i].name; i++)
  1412.              {
  1413.                /* Print in six columns. */
  1414.                if (printed == 6)
  1415.                  {
  1416.                    printed = 0;
  1417.                    printf ("\n");
  1418.                  }
  1419.      
  1420.                printf ("%s\t", commands[i].name);
  1421.                printed++;
  1422.              }
  1423.      
  1424.            if (printed)
  1425.              printf ("\n");
  1426.          }
  1427.      }
  1428.      
  1429.      /* Change to the directory ARG. */
  1430.      com_cd (arg)
  1431.           char *arg;
  1432.      {
  1433.        if (chdir (arg) == -1)
  1434.          perror (arg);
  1435.      
  1436.        com_pwd ("");
  1437.      }
  1438.      
  1439.      /* Print out the current working directory. */
  1440.      com_pwd (ignore)
  1441.           char *ignore;
  1442.      {
  1443.        char dir[1024];
  1444.      
  1445.        (void) getwd (dir);
  1446.      
  1447.        printf ("Current directory is %s\n", dir);
  1448.      }
  1449.      
  1450.      /* The user wishes to quit using this program.  Just set DONE non-zero. */
  1451.      com_quit (arg)
  1452.           char *arg;
  1453.      {
  1454.        done = 1;
  1455.      }
  1456.      
  1457.      /* Function which tells you that you can't do this. */
  1458.      too_dangerous (caller)
  1459.           char *caller;
  1460.      {
  1461.        fprintf (stderr,
  1462.                 "%s: Too dangerous for me to distribute.  Write it yourself.\n",
  1463.                 caller);
  1464.      }
  1465.      
  1466.      /* Return non-zero if ARG is a valid argument for CALLER, else print
  1467.         an error message and return zero. */
  1468.      int
  1469.      valid_argument (caller, arg)
  1470.           char *caller, *arg;
  1471.      {
  1472.        if (!arg || !*arg)
  1473.          {
  1474.            fprintf (stderr, "%s: Argument required.\n", caller);
  1475.            return (0);
  1476.          }
  1477.      
  1478.        return (1);
  1479.      }
  1480.  
  1481. File: readline.info,  Node: Concept Index,  Next: Function and Variable Index,  Prev: Programming with GNU Readline,  Up: Top
  1482.  
  1483. Concept Index
  1484. *************
  1485.  
  1486. * Menu:
  1487.  
  1488. * interaction, readline:                Readline Interaction.
  1489. * readline, function:                   Default Behaviour.
  1490.  
  1491. File: readline.info,  Node: Function and Variable Index,  Prev: Concept Index,  Up: Top
  1492.  
  1493. Function and Variable Index
  1494. ***************************
  1495.  
  1496. * Menu:
  1497.  
  1498. * char **completion_matches:            Completion Functions.
  1499. * char *filename_completion_function:   Completion Functions.
  1500. * char *rl_basic_word_break_characters: Completion Variables.
  1501. * char *rl_completer_word_break_characters: Completion Variables.
  1502. * char *rl_line_buffer:                 Function Writing.
  1503. * char *rl_special_prefixes:            Completion Variables.
  1504. * char *username_completion_function:   Completion Functions.
  1505. * Function *rl_attempted_completion_function: Completion Variables.
  1506. * Function *rl_completion_entry_function: How Completing Works.
  1507. * Function *rl_completion_entry_function: Completion Variables.
  1508. * Function *rl_ignore_some_completions_function: Completion Variables.
  1509. * int rl_bind_key:                      Binding Keys.
  1510. * int rl_bind_key_in_map:               Binding Keys.
  1511. * int rl_completion_query_items:        Completion Variables.
  1512. * int rl_end:                           Function Writing.
  1513. * int rl_filename_completion_desired:   Completion Variables.
  1514. * int rl_ignore_completion_duplicates:  Completion Variables.
  1515. * int rl_point:                         Function Writing.
  1516. * int rl_unbind_key:                    Binding Keys.
  1517. * int rl_unbind_key_in_map:             Binding Keys.
  1518. * Keymap rl_copy_keymap:                Keymaps.
  1519. * Keymap rl_make_bare_keymap:           Keymaps.
  1520. * Keymap rl_make_keymap:                Keymaps.
  1521. * readline ():                          Default Behaviour.
  1522. * rl_add_defun:                         Function Naming.
  1523. * rl_begin_undo_group:                  Allowing Undoing.
  1524. * rl_bind_key ():                       Default Behaviour.
  1525. * rl_complete:                          Completion Functions.
  1526. * rl_complete:                          How Completing Works.
  1527. * rl_complete_internal:                 Completion Functions.
  1528. * rl_end_undo_group:                    Allowing Undoing.
  1529. * rl_generic_bind:                      Binding Keys.
  1530. * rl_modifying:                         Allowing Undoing.
  1531. * rl_possible_completions:              Completion Functions.
  1532.  
  1533.  
  1534. Tag Table:
  1535. Node: Top999
  1536. Node: Command Line Editing1612
  1537. Node: Introduction and Notation2035
  1538. Node: Readline Interaction3047
  1539. Node: Readline Bare Essentials4186
  1540. Node: Readline Movement Commands5694
  1541. Node: Readline Killing Commands6585
  1542. Node: Readline Arguments8427
  1543. Node: Readline Init File9378
  1544. Node: Readline Init Syntax10079
  1545. Node: Commands For Moving11547
  1546. Node: Commands For History12172
  1547. Node: Commands For Text13248
  1548. Node: Commands For Killing14915
  1549. Node: Numeric Arguments16042
  1550. Node: Commands For Completion16485
  1551. Node: Miscellaneous Commands17209
  1552. Node: Readline Vi Mode17897
  1553. Node: Programming with GNU Readline19507
  1554. Node: Default Behaviour20212
  1555. Node: Custom Functions23435
  1556. Node: The Function Type24234
  1557. Node: Function Naming24867
  1558. Node: Keymaps26119
  1559. Node: Binding Keys27034
  1560. Node: Function Writing28335
  1561. Node: Allowing Undoing29776
  1562. Node: Custom Completers33278
  1563. Node: How Completing Works34034
  1564. Node: Completion Functions36841
  1565. Node: Completion Variables39174
  1566. Node: A Short Completion Example41951
  1567. Node: Concept Index53534
  1568. Node: Function and Variable Index53823
  1569. End Tag Table
  1570.